package article

import (
	"BookStack/internal/domain/article"
	"BookStack/pkg/utils"
)

// CreateArticleRequest 创建文章请求
type CreateArticleRequest struct {
	Title      string `json:"title" binding:"required,max=200"`
	Slug       string `json:"slug" binding:"required,max=255"`
	Content    string `json:"content"`
	Summary    string `json:"summary" binding:"max=500"`
	CategoryID uint   `json:"category_id"`
	CoverImage string `json:"cover_image" binding:"max=255"`
}

// UpdateArticleRequest 更新文章请求
type UpdateArticleRequest struct {
	ID         uint   `json:"id" binding:"required"`
	Title      string `json:"title" binding:"required,max=200"`
	Slug       string `json:"slug" binding:"required,max=255"`
	Content    string `json:"content"`
	Summary    string `json:"summary" binding:"max=500"`
	CategoryID uint   `json:"category_id"`
	CoverImage string `json:"cover_image" binding:"max=255"`
}

// SetStatusRequest 设置文章状态请求
type SetStatusRequest struct {
	ID     uint `json:"id" binding:"required"`
	Status uint `json:"status" binding:"required,oneof=0 1 2"`
}

// SetRecommendRequest 设置文章推荐状态请求
type SetRecommendRequest struct {
	ID          uint `json:"id" binding:"required"`
	IsRecommend bool `json:"is_recommend"`
}

// ArticleResponse 文章详情响应
type ArticleResponse struct {
	ID          uint   `json:"id"`
	Title       string `json:"title"`
	Slug        string `json:"slug"`
	Summary     string `json:"summary"`
	Content     string `json:"content"`
	CategoryID  uint   `json:"category_id"`
	AuthorID    uint   `json:"author_id"`
	CoverImage  string `json:"cover_image"`
	Views       uint   `json:"views"`
	Likes       uint   `json:"likes"`
	Comments    uint   `json:"comments"`
	Status      uint   `json:"status"`
	StatusText  string `json:"status_text"`
	IsRecommend bool   `json:"is_recommend"`
	CreatedAt   string `json:"created_at"`
	UpdatedAt   string `json:"updated_at"`
	PublishedAt string `json:"published_at,omitempty"`
}

// ArticleBriefResponse 文章简要信息响应
type ArticleBriefResponse struct {
	ID          uint   `json:"id"`
	Title       string `json:"title"`
	Slug        string `json:"slug"`
	Summary     string `json:"summary"`
	CategoryID  uint   `json:"category_id"`
	AuthorID    uint   `json:"author_id"`
	CoverImage  string `json:"cover_image"`
	Views       uint   `json:"views"`
	Likes       uint   `json:"likes"`
	Comments    uint   `json:"comments"`
	Status      uint   `json:"status"`
	StatusText  string `json:"status_text"`
	IsRecommend bool   `json:"is_recommend"`
	CreatedAt   string `json:"created_at"`
	PublishedAt string `json:"published_at,omitempty"`
}

// ArticleListResponse 文章列表响应
type ArticleListResponse struct {
	Total int64                   `json:"total"`
	List  []*ArticleBriefResponse `json:"list"`
}

// FromEntity 将领域实体转换为DTO
func FromEntity(a *article.Article) *ArticleResponse {
	return &ArticleResponse{
		ID:          a.ID,
		Title:       a.Title,
		Slug:        a.Slug,
		Summary:     a.Summary,
		Content:     a.Content,
		CategoryID:  a.CategoryID,
		AuthorID:    a.AuthorID,
		CoverImage:  a.CoverImage,
		Views:       a.Views,
		Likes:       a.Likes,
		Comments:    a.Comments,
		Status:      a.Status,
		StatusText:  getStatusText(a.Status),
		IsRecommend: a.IsRecommend,
		CreatedAt:   utils.FormatTime(a.CreatedAt),
		UpdatedAt:   utils.FormatTime(a.UpdatedAt),
		PublishedAt: utils.FormatTime(a.PublishedAt),
	}
}

// FromEntityBrief 将领域实体转换为简要DTO
func FromEntityBrief(a *article.Article) *ArticleBriefResponse {
	return &ArticleBriefResponse{
		ID:          a.ID,
		Title:       a.Title,
		Slug:        a.Slug,
		Summary:     a.Summary,
		CategoryID:  a.CategoryID,
		AuthorID:    a.AuthorID,
		CoverImage:  a.CoverImage,
		Views:       a.Views,
		Likes:       a.Likes,
		Comments:    a.Comments,
		Status:      a.Status,
		StatusText:  getStatusText(a.Status),
		IsRecommend: a.IsRecommend,
		CreatedAt:   utils.FormatTime(a.CreatedAt),
		PublishedAt: utils.FormatTime(a.PublishedAt),
	}
}

// BatchFromEntities 批量将领域实体转换为DTO
func BatchFromEntities(articles []*article.Article) []*ArticleResponse {
	result := make([]*ArticleResponse, len(articles))
	for i, a := range articles {
		result[i] = FromEntity(a)
	}
	return result
}

// BatchFromEntitiesBrief 批量将领域实体转换为简要DTO
func BatchFromEntitiesBrief(articles []*article.Article) []*ArticleBriefResponse {
	result := make([]*ArticleBriefResponse, len(articles))
	for i, a := range articles {
		result[i] = FromEntityBrief(a)
	}
	return result
}

// ToListResponse 将领域实体集合转换为列表响应
func ToListResponse(articles []*article.Article, total int64) *ArticleListResponse {
	return &ArticleListResponse{
		Total: total,
		List:  BatchFromEntitiesBrief(articles),
	}
}

// getStatusText 获取状态文本
func getStatusText(status uint) string {
	switch status {
	case article.StatusDraft:
		return "草稿"
	case article.StatusPublished:
		return "已发布"
	case article.StatusArchived:
		return "已归档"
	default:
		return "未知"
	}
}
